package cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.contractpaymentplan.vo.ContractPaymentPlanSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractpaymentplan.ContractPaymentPlanDO;
import cn.iocoder.yudao.module.erp.service.contractpaymentplan.ContractPaymentPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 合同付款计划")
@RestController
@RequestMapping("/erp/contract-payment-plan")
@Validated
public class  ContractPaymentPlanController {

    @Resource
    private ContractPaymentPlanService contractPaymentPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建合同付款计划")
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:create')")
    public CommonResult<Long> createContractPaymentPlan(@Valid @RequestBody ContractPaymentPlanSaveReqVO createReqVO) {
        return success(contractPaymentPlanService.createContractPaymentPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同付款计划")
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:update')")
    public CommonResult<Boolean> updateContractPaymentPlan(@Valid @RequestBody ContractPaymentPlanSaveReqVO updateReqVO) {
        contractPaymentPlanService.updateContractPaymentPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同付款计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:delete')")
    public CommonResult<Boolean> deleteContractPaymentPlan(@RequestParam("id") Long id) {
        contractPaymentPlanService.deleteContractPaymentPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除合同付款计划")
                @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:delete')")
    public CommonResult<Boolean> deleteContractPaymentPlanList(@RequestParam("ids") List<Long> ids) {
        contractPaymentPlanService.deleteContractPaymentPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同付款计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:query')")
    public CommonResult<ContractPaymentPlanRespVO> getContractPaymentPlan(@RequestParam("id") Long id) {
        return success(contractPaymentPlanService.getContractPaymentPlan(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同付款计划分页")
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:query')")
    public CommonResult<PageResult<ContractPaymentPlanRespVO>> getContractPaymentPlanPage(@Valid ContractPaymentPlanPageReqVO pageReqVO) {
        PageResult<ContractPaymentPlanDO> pageResult = contractPaymentPlanService.getContractPaymentPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractPaymentPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同付款计划 Excel")
    @PreAuthorize("@ss.hasPermission('erp:contract-payment-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractPaymentPlanExcel(@Valid ContractPaymentPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractPaymentPlanDO> list = contractPaymentPlanService.getContractPaymentPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.appWrite(response, "合同付款计划.xls", "数据", ContractPaymentPlanRespVO.class,
                        BeanUtils.toBean(list, ContractPaymentPlanRespVO.class));
    }

}